package cn.lau.pp.module.vr.service.job;

import cn.lau.pp.framework.common.util.date.LocalDateTimeUtils;
import cn.lau.pp.module.vr.dal.dataobject.device.DeviceDO;
import cn.lau.pp.module.vr.dal.dataobject.device.DeviceGameDO;
import cn.lau.pp.module.vr.dal.dataobject.game.GameDO;
import cn.lau.pp.module.vr.dal.dataobject.order.OrderDO;
import cn.lau.pp.module.vr.dal.mysql.device.DeviceGameMapper;
import cn.lau.pp.module.vr.dal.mysql.device.DeviceMapper;
import cn.lau.pp.module.vr.dal.mysql.game.GameMapper;
import cn.lau.pp.module.vr.dal.mysql.order.OrderMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

@Component
public class VrJob {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private DeviceGameMapper deviceGameMapper;
    @Resource
    private DeviceMapper deviceMapper;

    @Resource
    private GameMapper gameMapper;
    /**
     *  定时更新设备状态，生成订单
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    private void vrOrderJob() {

        List<DeviceGameDO> deviceGameDOS = deviceGameMapper.selectList();
        for (DeviceGameDO deviceGameDo : deviceGameDOS) {
            LocalDateTime createTime = deviceGameDo.getCreateTime();
            GameDO gameDO = gameMapper.selectById(deviceGameDo.getGameId());
            DeviceDO deviceDO = deviceMapper.selectById(deviceGameDo.getDeviceId());
            int time = gameDO.getTime();
            LocalDateTime localDateTime = createTime.plusMinutes(time);
            if(localDateTime.isAfter(LocalDateTimeUtils.getToday())){
                deviceGameMapper.deleteById(deviceGameDo.getId());
                deviceDO.setStatus(0);
                deviceMapper.updateById(deviceDO);
                OrderDO orderDO = new OrderDO();
                orderDO.setOrderCode("SF"+ UUID.randomUUID());
                orderDO.setAmount(gameDO.getAmount());
                orderDO.setDeviceId(deviceGameDo.getDeviceId());
                orderDO.setGameId(deviceGameDo.getGameId());
                orderDO.setPlayTime(time);
                orderMapper.insert(orderDO);
            }


        }


    }
}
